#include "menu.h"
int Random(int min , int max)
{
return (rand()% (max-min))+min;
}

//fonction chargement allegro
void initialiser()
{
allegro_init();
install_keyboard();
set_color_depth(32); //palette de couleur 32
set_gfx_mode(GFX_AUTODETECT_WINDOWED,800,593,0,0);
}

//fonction menu début
void chargerMenu(Menu *monMenu)
{
monMenu->images[0]=load_bitmap("menu_PLAY.bmp",NULL);
monMenu->images[1]=load_bitmap("menu_help1.bmp",NULL);
monMenu->images[2]=load_bitmap("menu_EXIT.bmp",NULL);
int i;
for(i=0;i<NB_IMAGES_MENU;i++)
{
if (monMenu->images[i] == NULL)
{
allegro_message("could not load images");
allegro_exit();
}
}
monMenu->elementSelectioner=0;
}
void menu(Menu *monMenu, int *choix)
{
int state=0;
while(!state)
{
static int touchePresse;
if(keypressed())
{
touchePresse=readkey();
if ((touchePresse>>8) == KEY_DOWN)
{
monMenu->elementSelectioner=(monMenu->elementSelectioner+1)%3;
}
else if(touchePresse>>8 == KEY_UP)
{
if(monMenu->elementSelectioner==0) monMenu->elementSelectioner=2;
else
monMenu->elementSelectioner=(monMenu->elementSelectioner-1)%3;
}
else if(touchePresse>>8 == KEY_ENTER)
state=1;
}
draw_sprite(screen, monMenu->images[monMenu->elementSelectioner], 0, 0);
}
*choix=monMenu->elementSelectioner;
}


//fonction pause en plein partie
void MenuPause(int * continuer, int back_indice, int x, int direction,int score)
{
    BITMAP *menu2[3];
    int selected=0,touchePresse,state=0;
    menu2[0]=load_bitmap("menu2_continue.bmp", NULL);
    menu2[1]=load_bitmap("menu2_save.bmp", NULL);
    menu2[2]=load_bitmap("menu2_exit.bmp", NULL);


    while(!state)
    {
        if(keypressed())
        {
            touchePresse=readkey();
            if ((touchePresse>>8) == KEY_DOWN)
            {
                selected=(selected+1)%3;
            }
            else if(touchePresse>>8 == KEY_UP)
            {
                selected=(selected+2)%3;
            }
            else if(touchePresse>>8 == KEY_ENTER)
                state=1;
        }
        draw_sprite(screen, menu2[selected], 0, 0);
        rest(10);
    }

    switch(selected)
    {
    case 0:
        break;
    case 1:
        set_config_int("Game","back_indice",back_indice);
        set_config_int("Game","x",x);
        set_config_int("Game","direction",direction);
        set_config_int("Game","score",score);
        flush_config_file();
        break;
    case 2:
        *continuer=1;
        break;
    }

    for(selected=0; selected<3; selected++)
        destroy_bitmap(menu2[selected]);
}

// fonction d'arret de jeu
void gameover (BITMAP *buffer, int * continuer)
{
    BITMAP *gameover;
    gameover=load_bitmap("gameover.bmp",NULL);
    while(!key[KEY_ENTER])
    {
draw_sprite(screen, gameover, 0, 0);
}
if (key[KEY_ENTER])
*continuer=1;
}
